<?php 
class Admin_AccountController extends Zend_Controller_Action{
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	public function init() {
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
	}
	public function indexAction() {
		$mAccount = new Admin_Model_Account();
		if ($mAccount->hasIdentity()) {
			$this->view->fullName = $mAccount->getName();
			if(!$mAccount->isAdmin()) {
				$this->_redirect('/account/?ERR_CODE=200');
			}
		} else {
			$this->_redirect('/admin/account/login/');
		}
	}
	public function loginAction(){
		$mAccount = new Admin_Model_Account();
		if(!$mAccount->hasIdentity()) {
			if ($this->_request->isPost()) {
				// redirect processing
				if ($mAccount->login($this->_arrParam)) {
					if($mAccount->isAdmin()) {
						$this->_redirect('/admin/account/');
					} else {
						$this->_redirect('/account/?ERR_CODE=200');
					}
				} else {
					$this->_redirect('/account/login/?ERR_CODE=100');
				}
			}
		} else {			
			if(!$mAccount->isAdmin()) {
				$this->_redirect('/account/?ERR_CODE=200');
			} else {
				$this->_redirect('/admin/account/login/');
			}
		}

	}
	public function logoutAction() {
		$mAccount = new Admin_Model_Account();
		if (!$mAccount->hasIdentity()) {
			$this->_redirect('/admin/account/login/');
		} else {
			if(!$mAccount->isAdmin()) {
				$this->_redirect('/account/?ERR_CODE=200');
			} else {
				$mAccount->logout();
			}			
		}
	}

}